package com.cndw.kungfu.event.once;

import java.util.ArrayList;
import java.util.List;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Scene;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.activity.ModeSceneWall;

/**
 * 删除玩家某个状态的任务
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-9-19 上午11:10:47
 */
public class PlayerWallTask implements Runnable {

	private List<Player> players = new ArrayList<Player>(1);
	private List<Player> targets = new ArrayList<Player>(1);
	private int status;
	private int winer;
	private Scene scene;

	/**
	 * 
	 * @param status
	 *            需要删除的状态
	 * @param players
	 */
	public PlayerWallTask(int status, int seconds, int winer, Scene scene, Player player, Player target) {
		this.winer = winer;
		this.scene = scene;
		this.status = status;

		if (BlankUtils.isBlank(player.getTeam())) {
			this.players.add(player);
		} else {
			this.players.addAll(player.getTeam().getBattlePlayers());
		}

		if (BlankUtils.isBlank(target.getTeam())) {
			this.targets.add(target);
		} else {
			this.targets.addAll(target.getTeam().getBattlePlayers());
		}

		init(seconds);
	}

	@Override
	public void run() {
		try {
			// 塞外设置战败玩家的死亡时间
			if (scene.isCityWall()) {
				if (winer == 1) {
					ModeSceneWall.killPlayer(status, players, targets);
				} else {
					ModeSceneWall.killPlayer(status, targets, players);
				}
			} else {
				for (Player player : players) {
					player.stateRemove(status);
					Context.getPlayerServer().broadcaseState(player);
				}

				for (Player target : targets) {
					target.stateRemove(status);
					Context.getPlayerServer().broadcaseState(target);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void init(int seconds) {
		Context.getTimerServer().scheduleSeconds(this, seconds);
	}

}
